package com.example.taobaounion.presenter.impl

import com.example.taobaounion.model.bean.SelectCategoryBean
import com.example.taobaounion.model.bean.SelectCategoryContentBean
import com.example.taobaounion.net.API
import com.example.taobaounion.presenter.ISelectPresenter
import com.example.taobaounion.view.ISelectCallBack
import com.example.taobaounion.utils.RetrofitManager
import com.example.taobaounion.utils.UrlUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.net.HttpURLConnection

/**
 *@author : Administrator
 *@descreption : 精选页面的presenter实现类
 */
class SelectPresenterImpl : ISelectPresenter {
    enum class State{
        LOADING,SUCCESS,ERROR,NONE
    }

    private var mData: SelectCategoryBean? = null
    var mCurrentState =
        State.NONE
    var mSelectCallBack :ISelectCallBack?= null

    override fun getCategory() {
        mCurrentState =
            State.LOADING
        val request = getNetRequest()
        val call =  request.getSelectCategoryData()
        call.enqueue(object : Callback<SelectCategoryBean> {
            override fun onFailure(call: Call<SelectCategoryBean>, t: Throwable) {
                mCurrentState =
                    State.ERROR
            }

            override fun onResponse(call: Call<SelectCategoryBean>, response: Response<SelectCategoryBean>) {
                mData = response.body()
                if (response.code() == HttpURLConnection.HTTP_OK){
                    if (mSelectCallBack!=null){
                        mSelectCallBack!!.getDataSuccess(response.body())
                    }else{
                        mCurrentState =
                            State.SUCCESS
                    }
                }else{
                    mCurrentState =
                        State.ERROR
                }
            }

        })

    }

    private fun getNetRequest(): API {
        val retrofit = RetrofitManager.getInstance().retrofit
        return retrofit.create(API::class.java)
    }

    override fun getCategoryContent(dataBean: SelectCategoryBean.DataBean) {
        val request = getNetRequest()
        val url = UrlUtil.getSelectCategoryContentDataUrl(dataBean.favorites_id)
        val call = request.getSelectCategoryContentData(url)
        call.enqueue(object : Callback<SelectCategoryContentBean> {
            override fun onFailure(call: Call<SelectCategoryContentBean>, t: Throwable) {
                mSelectCallBack?.getContentDataError()

            }

            override fun onResponse(call: Call<SelectCategoryContentBean>, response: Response<SelectCategoryContentBean>) {
                val data = response.body()?.data?.tbk_uatm_favorites_item_get_response?.results
                if (response.code()==HttpURLConnection.HTTP_OK){
                    mSelectCallBack?.getContentDataSuccess(data)
                }else{
                    mSelectCallBack?.getContentDataError()
                }
            }

        })

    }

    override fun registerCallBack(selectCallBack: ISelectCallBack) {
        this.mSelectCallBack = selectCallBack
        if (mCurrentState!= State.NONE){
        when(mCurrentState){
            State.ERROR -> run{
                mSelectCallBack?.getDataError()
            }
            State.SUCCESS -> run {
                mSelectCallBack?.getDataSuccess(mData)
            }
            else -> {
                mSelectCallBack?.loading()
            }
        }
        }
    }

    override fun unRegisterCallBack() {
        this.mSelectCallBack = null
    }
}